package com.whmnx;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Administrator on 2016/8/10.
 */
public class SPUtils {

    private static final String SP_FILE_NAME = "config";
    private static final String USERID = "userId";
    private static final String USER_PHOTO = "user_photo";
    private static final String USER_NAME = "user_name";
    private static final String NAME = "name";
    private static final String ADDRESSID = "addressid";
    private static final String FIRST = "first";
    private static final String RECORD_SEARCH = "record_search";
    private static final String COMMENT_FLAG = "CommentFlag";
    private static final String HIT_FLAG = "hitflag";
    private static final String AUTO_FLAG = "auto_flag";
    private static final String SHARE_FLAG = "share_flag";
    private static final String AUTH = "auth";
    private static final String ISWIFI = "isWiFi";
    private static final String USER_TYPE = "USER_TYPE";
    private static final String USER_NUM = "USER_NUM";


    private static Context mContext;
    private static SPUtils instance = null;
    private static SharedPreferences sp;

    private SPUtils(Context context) {
        mContext = context;
        sp = mContext.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    }

    public synchronized static SPUtils newIntance(Context context) {

        if (instance == null) {
            instance = new SPUtils(context);
        }

        return instance;
    }


    public String getUserId() {
        String userName = sp.getString(USERID, null);
        return userName;
    }

    public void setUserId(String userId) {
        sp.edit().putString(USERID, userId).commit();
    }

    public String getPhoto() {
        String phton = sp.getString(USER_PHOTO, null);
        return phton;
    }

    public void setPhoto(String photo) {
        sp.edit().putString(USER_PHOTO, photo).commit();
    }

    public String getUserName() {
        String phton = sp.getString(USER_NAME, "");
        return phton;
    }

    public void setUserName(String user_name) {
        sp.edit().putString(USER_NAME, user_name).commit();
    }

    public String getName() {
        String phton = sp.getString(NAME, "");
        return phton;
    }

    public void setName(String user_name) {
        sp.edit().putString(NAME, user_name).commit();
    }

    public String getAddress() {
        return sp.getString(ADDRESSID, "");
    }

    public void setAddress(String addressid) {
        sp.edit().putString(ADDRESSID, addressid).commit();
    }

    public void setDay(String day) {
        sp.edit().putString("day", day).commit();
    }

    public String getDay() {
        return sp.getString("day", "");
    }

    public void setNetType(boolean isWiFi) {
        sp.edit().putBoolean(ISWIFI, isWiFi).commit();
    }

    public boolean getNetType() {
        return sp.getBoolean(ISWIFI, false);
    }

    /**
     * 第一次启动
     *
     * @return
     */
    public boolean getFirst() {
        return sp.getBoolean(FIRST, true);
    }

    /**
     * 第一次启动
     */
    public void setFirst() {
        sp.edit().putBoolean(FIRST, false).commit();
    }


    public boolean getAdmin() {
        return getUserType() == 1 ? true : false;
    }

    /**
     * 获取搜索历史
     *
     * @return
     */
    public String getRecordSearch() {
        return sp.getString(RECORD_SEARCH, "");
    }

    /**
     * 设置搜索历史
     *
     * @param recordSearch
     */
    public void setRecordSearch(String recordSearch) {
        sp.edit().putString(RECORD_SEARCH, recordSearch).commit();
    }

    public void clearRecord() {
        sp.edit().putString(RECORD_SEARCH, "").commit();
    }

    /**
     * 清空数据
     */
    public void clear() {
        sp.edit().clear().commit();
    }

    /**
     * 设置评论通知
     *
     * @param bool
     */
    public void setCommentFlag(boolean bool) {
        sp.edit().putBoolean(COMMENT_FLAG, bool).commit();
    }

    /**
     * 评论通知
     *
     * @return
     */
    public boolean getCommentFlag() {
        return sp.getBoolean(COMMENT_FLAG, false);
    }

    /**
     * 设置点赞通知
     *
     * @param bool
     */
    public void setHitFlag(boolean bool) {
        sp.edit().putBoolean(HIT_FLAG, bool).commit();
    }

    /**
     * 点赞通知
     *
     * @return
     */
    public boolean getHitFlag() {
        return sp.getBoolean(HIT_FLAG, false);
    }

    /**
     * 设置自动播放
     *
     * @param bool
     */
    public void setAutoFlag(boolean bool) {
        sp.edit().putBoolean(AUTO_FLAG, bool).commit();
    }

    /**
     * 自动播放
     *
     * @return
     */
    public boolean getAutoFlag() {
        return sp.getBoolean(AUTO_FLAG, false);
    }

    /**
     * 设置分享通知
     *
     * @param bool
     */
    public void setShareFlag(boolean bool) {
        sp.edit().putBoolean(SHARE_FLAG, bool).commit();
    }

    /**
     * 分享通知
     *
     * @return
     */
    public boolean getShareFlag() {
        return sp.getBoolean(SHARE_FLAG, false);
    }

    /**
     * 设置实名认证
     *
     * @param b
     */
    public void setAuth(boolean b) {
        sp.edit().putBoolean(AUTH, b).commit();
    }

    /**
     * 是否实名认证
     *
     * @return
     */
    public boolean isAuth() {
        return sp.getBoolean(AUTH, false);
    }

    public void setUserType(int i) {
        sp.edit().putInt(USER_TYPE, i).commit();
    }

    public int getUserType() {
        return sp.getInt(USER_TYPE, 0);
    }

    public void setUserNum(int i) {
        sp.edit().putInt(USER_NUM, i).commit();
    }

    public int getUserNum() {
        return sp.getInt(USER_NUM, 0);
    }
}
